<template>
    <div style="border: 1px solid #ccc; margin-top: 10px; z-index: 1001">
        <Toolbar
            style="border-bottom: 1px solid #ccc"
            :editor="editor"
            :defaultConfig="toolbarConfig"
            :mode="mode"
        />
        <Editor
            v-model="myValue"
            style="height: 500px; overflow-y: hidden"
            :defaultConfig="editorConfig"
            :mode="mode"
            @onCreated="onCreated"
        />
    </div>
</template>
<script>
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
import "@wangeditor/editor/dist/css/style.css";

export default {
    name: "VWangeditor",
    components: { Editor, Toolbar },
    model: {
        prop: "value",
        event: "change",
    },
    props: {
        value: [String],
    },
    data() {
        return {
            editor: null,
            toolbarConfig: {},
            editorConfig: { placeholder: "请输入内容..." },
            mode: "default", // or 'simple'
        };
    },
    computed: {
        myValue: {
            get() {
                return this.value;
            },
            set(val) {
                this.$emit("change", val);
            },
        },
    },
    beforeDestroy() {
        const editor = this.editor;
        if (editor == null) return;
        editor.destroy(); // 组件销毁时，及时销毁编辑器
    },
    methods: {
        onCreated(editor) {
            this.editor = Object.seal(editor); // 一定要用 Object.seal() ，否则会报错
        },
    },
};
</script>

<style></style>
